package com.hrms.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hrms.entity.Room;
import com.hrms.mapper.RoomMapper;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.Map;

@Service
public class RoomServiceImpl extends ServiceImpl<RoomMapper, Room> implements RoomService {
    public List<Map<String, Object>> selectAll() {
        // 返回表room的所有记录
        // 排除占位记录即room_id为000的记录
        QueryWrapper<Room> wrapper = new QueryWrapper<>();
        wrapper.ne("room_id",0);

        return listMaps(wrapper);
    }

    public List<Map<String, Object>> selectSpareRoom() {
        // 返回表room中room_status为“空闲”的记录
        QueryWrapper<Room> wrapper = new QueryWrapper<>();
        wrapper.eq("room_status", "空闲");

        return listMaps(wrapper);
    }

    public boolean idExist(Room room) {
        // 查询房间号是否存在
        QueryWrapper<Room> wrapper = new QueryWrapper<>();
        wrapper.eq("room_id", room.getRoom_id())
                .ne("room_status","已删除");
        int count = (int) count(wrapper);

        if(count == 0) return false;
        else return true;
    }

    public boolean checkStatue(Room room) {
        // 根据room_id和room_status查询房间号状态是否匹配
        QueryWrapper<Room> wrapper = new QueryWrapper<>();
        wrapper.eq("room_id", room.getRoom_id())
                .eq("room_status", room.getRoom_status());
        int count = (int) count(wrapper);

        if(count == 0) return false;
        else return true;
    }

    public boolean updateStatue(Room room) {
        // 根据room_id修改room_status
        // true表示修改成功
        QueryWrapper<Room> wrapper = new QueryWrapper<>();
        wrapper.eq("room_id", room.getRoom_id())
                .ne("room_status", "已删除");

        return update(room, wrapper);
    }

    public boolean add(Room room) {
        // 在表room中添加一条记录
        // true表示添加成功
        return save(room);
    }

    public boolean update(Room room) {
        // 根据room_id和room_status修改room_type和room_price
        // true表示修改成功
        QueryWrapper<Room> wrapper = new QueryWrapper<>();
        wrapper.eq("room_id", room.getRoom_id())
                .eq("room_status", room.getRoom_status());

        return update(room, wrapper);
    }

    public String selectRoomType(Room room) {
        // 根据room_id返回room_type
        QueryWrapper<Room> wrapper = new QueryWrapper<>();
        wrapper.eq("room_id", room.getRoom_id())
                .ne("room_status", "已删除")
                .select("room_type");

        List<Map<String, Object>> list = listMaps(wrapper);

        return list.get(0).get("room_type").toString();
    }

    public String selectRoomPrice(Room room) {
        // 根据room_id返回room_price
        QueryWrapper<Room> wrapper = new QueryWrapper<>();
        wrapper.eq("room_id", room.getRoom_id())
                .ne("room_status", "已删除")
                .select("room_price");

        List<Map<String, Object>> list = listMaps(wrapper);

        return list.get(0).get("room_price").toString();
    }

    public String selectBeforeRoomType(Room room) {
        // 根据room_id返回room_type
        // 状态为已删除
        QueryWrapper<Room> wrapper = new QueryWrapper<>();
        wrapper.eq("room_id", room.getRoom_id())
                .eq("room_status", "已删除")
                .select("room_type");

        List<Map<String, Object>> list = listMaps(wrapper);

        return list.get(0).get("room_type").toString();
    }

    public String selectBeforeRoomPrice(Room room) {
        // 根据room_id返回room_price
        // 状态为已删除
        QueryWrapper<Room> wrapper = new QueryWrapper<>();
        wrapper.eq("room_id", room.getRoom_id())
                .eq("room_status", "已删除")
                .select("room_price");

        List<Map<String, Object>> list = listMaps(wrapper);

        return list.get(0).get("room_price").toString();
    }

    public int typeCount(String room_type) {
        // 根据room_type返回数量
        QueryWrapper<Room> wrapper = new QueryWrapper<>();
        wrapper.eq("room_type", room_type)
                .eq("room_status", "空闲")
                .select("room_type");

        List<Map<String, Object>> list = listMaps(wrapper);
        return list.size();
    }

    public String selectRoomPricebyType(String room_type) {
        // 根据room_type返回room_price
        QueryWrapper<Room> wrapper = new QueryWrapper<>();
        wrapper.eq("room_type", room_type)
                .ne("room_status", "已删除")
                .select("room_price");

        List<Map<String, Object>> list = listMaps(wrapper);

        if(list.isEmpty()) return "";
        else return list.get(0).get("room_price").toString();
    }

    public String getOneRoomId(Room room) {
        // 根据room_type返回一个room_id
        QueryWrapper<Room> wrapper = new QueryWrapper<>();
        wrapper.eq("room_type", room.getRoom_type())
                .eq("room_status", "空闲")
                .select("room_id");

        List<Map<String, Object>> list = listMaps(wrapper);
        return list.get(0).get("room_id").toString();
    }

    public boolean delete(Room room) {
        // 根据room_id在表room中删除一条记录
        // true表示删除成功

        QueryWrapper<Room> wrapper = new QueryWrapper<>();
        wrapper.eq("room_id", room.getRoom_id())
                .ne("room_status", "已删除");
        return remove(wrapper);
    }

    public boolean checkAllStatusByType(Room room) {
        // 根据room_type查询是否有非空闲客房

        QueryWrapper<Room> wrapper = new QueryWrapper<>();
        wrapper.eq("room_type", room.getRoom_type())
                .ne("room_status", "空闲")
                .ne("room_status", "已删除");

        List<Map<String, Object>> list = listMaps(wrapper);

        if(list.isEmpty()) return true;
        else return false;
    }

    public boolean updateAllPriceByType(Room room) {
        // 根据room_type修改所有的room_price

        QueryWrapper<Room> wrapper = new QueryWrapper<>();
        wrapper.eq("room_type", room.getRoom_type())
                .ne("room_status", "已删除");

        Room newRoom = new Room();
        newRoom.setRoom_price(room.getRoom_price());

        return update(newRoom, wrapper);
    }
}
